package com.chushouya.order.task;

import com.general.framework.job.starter.TaskUtil;
import com.chushouya.order.service.common.ManageDayStatTaskService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 经营数据统计TASK
 */
@Component
@Slf4j
public class ManageStatTask {

    @Resource
    private ManageDayStatTaskService manageDayStatTaskService;

    @XxlJob(value = "manageTodayStatJobHandler")
    public ReturnT<String> manageTodayStatJobHandler() {
        final String jobName = "运营数据今日订单统计";
        String param = XxlJobHelper.getJobParam();
        final Long startTime = TaskUtil.taskStart(jobName, param);
        StatParam statParam = new StatParam(param).invoke();
        manageDayStatTaskService.manageTodayStat(statParam.getDate());
        TaskUtil.taskEnd(jobName, param, startTime);
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "manageYesterdayStatJobHandler")
    public ReturnT<String> manageYesterdayStatJobHandler() {
        final String jobName = "运营数据昨日订单统计";
        String param = XxlJobHelper.getJobParam();
        final Long startTime = TaskUtil.taskStart(jobName, param);
        StatParam statParam = new StatParam(param).invoke();
        manageDayStatTaskService.manageYesterdayStat(statParam.getDate());
        TaskUtil.taskEnd(jobName, param, startTime);
        return ReturnT.SUCCESS;
    }

    @Data
    private class StatParam {

        private String param;

        private Long kefuId;

        private Date date;

        private StatParam(String param) {
            this.param = param;
        }

        private StatParam invoke() {
            kefuId = null;
            date = null;
            // xxl-job 手动调用传参
            if (Strings.isNotBlank(param)) {
                kefuId = TaskUtil.getParamId(param.split(",")[0]);
                date = TaskUtil.getParamDate(param.split(",")[1]);
            }
            return this;
        }
    }
}
